//
//  CTMainViewController.m
//  CallTaxi
//
//  Created by yuan fang on 12-10-9.
//  Copyright (c) 2012年 MoPay. All rights reserved.
//

#import "CTMainViewController.h"
#import "CTMLeftViewController.h"
#import "CTMMiddleViewController.h"
#import "CLViewController.h"
#import "CLImage.h"
#import "PrettyNavigationBar.h"

#define kChangeMiddleViewNotification @"changeMiddleView"
#define kMoveMiddleViewNotification @"moveMiddleView"

#define kSelectMoveAnimationTimes 0.2
#define kSelectMoveAnimationTimeOne 0.35

@interface CTMainViewController ()
// 切换中间视图
- (void)didReceiveChangeMiddleViewNotification:(NSNotification *)notificaton;
// 创建中间视图
- (UINavigationController *)createMiddleCtrl;
@end

@implementation CTMainViewController

@synthesize leftViewCtrl;
@synthesize middleViewCtrl;
@synthesize midType;


- (void)loadView {

    // BaseView
    
    NSLog(@"main screen:%f", [[UIScreen mainScreen] bounds].size.height);
    UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    baseView.backgroundColor = [UIColor whiteColor];
    self.view = baseView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    //初始视图进入到ToDo
    midType = CL_JSTL;
    // 控制视图
    [self.view addSubview:self.leftViewCtrl.view];
    // 主视图
    self.middleViewCtrl = [self createMiddleCtrl];
    [self.view insertSubview:self.middleViewCtrl.view aboveSubview:self.leftViewCtrl.view];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveChangeMiddleViewNotification:)
                                                 name:kChangeMiddleViewNotification
                                               object:nil];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kChangeMiddleViewNotification object:nil];
}

#pragma mark -
#pragma mark method
- (CTMLeftViewController *)leftViewCtrl {
    if (!leftViewCtrl) {
        CTMLeftViewController *leftCtrl = [[CTMLeftViewController alloc] init];
        leftViewCtrl = leftCtrl;
    }
    
    return leftViewCtrl;
}

// 创建中间视图
- (UINavigationController *)createMiddleCtrl {
    // 创建主视图
     CTMMiddleViewController *midCtrl = [[CLViewController alloc] initWithNibName:@"CLViewController" bundle:nil];
    switch (midType) {
        case CL_JSTL:
            midCtrl.midType = CL_JSTL;
            ((CLViewController *)midCtrl).forumContentType = JSTL_SECTION;
            ((CLViewController *)midCtrl).title = [leftViewCtrl.dataArray objectAtIndex:0];
            break;
        case CL_XSDDWM:
            midCtrl.midType = CL_XSDDWM;
            ((CLViewController *)midCtrl).forumContentType = XSDDWM_SECTION;
            ((CLViewController *)midCtrl).title = [leftViewCtrl.dataArray objectAtIndex:1];
            break;
        case CL_DGEDQZ:
            midCtrl.midType = CL_DGEDQZ;
            ((CLViewController *)midCtrl).forumContentType = DGEDQZ_SECTION;
            ((CLViewController *)midCtrl).title = [leftViewCtrl.dataArray objectAtIndex:2];
            break;
        case CL_WX:
            midCtrl.midType = CL_WX;
            ((CLViewController *)midCtrl).forumContentType = CRWX_SECTION;
            ((CLViewController *)midCtrl).title = [leftViewCtrl.dataArray objectAtIndex:3];
            break;
        case CL_YZWM:
            midCtrl.midType = CL_YZWM;
            ((CLViewController *)midCtrl).forumContentType = YZWM_SECTION;
            ((CLViewController *)midCtrl).title = [leftViewCtrl.dataArray objectAtIndex:4];
            break;
        case CL_YZYM:
            midCtrl.midType = CL_YZYM;
            ((CLViewController *)midCtrl).forumContentType = YZYM_SECTION;
            ((CLViewController *)midCtrl).title = [leftViewCtrl.dataArray objectAtIndex:5];
            break;
        
        case CL_YZWM_Z:
            midCtrl.midType = CL_YZWM_Z;
            ((CLViewController *)midCtrl).forumContentType = YZWM_Z_SECTION;
            ((CLViewController *)midCtrl).title = [leftViewCtrl.dataArray objectAtIndex:6];
            break;
            
        case CL_YZYM_Z:
            midCtrl.midType = CL_YZYM_Z;
            ((CLViewController *)midCtrl).forumContentType = YZYM_Z_SECTION;
            ((CLViewController *)midCtrl).title = [leftViewCtrl.dataArray objectAtIndex:7];
            break;
            
        case CL_OM:
            midCtrl.midType = CL_OM;
            ((CLViewController *)midCtrl).forumContentType = OM_SECTION;
            ((CLViewController *)midCtrl).title = [leftViewCtrl.dataArray objectAtIndex:8];
            break;
            
        case CL_OM_Z:
            midCtrl.midType = CL_OM_Z;
            ((CLViewController *)midCtrl).forumContentType = OM_Z_SECTION;
            ((CLViewController *)midCtrl).title = [leftViewCtrl.dataArray objectAtIndex:9];
            break;

        default:
            break;
    }
    
    UINavigationController *midNavCtrl = [[UINavigationController alloc] initWithRootViewController:midCtrl] ;
//    midNavCtrl.navigationBar = [[PrettyNavigationBar alloc] init] ;
//    [midNavCtrl.navigationBar setBackgroundImage:[UIImage imageNamed:kNavBarBgImage] forBarMetrics:UIBarMetricsDefault];
    
    return midNavCtrl;
}

// 切换中间视图
- (void)didReceiveChangeMiddleViewNotification:(NSNotification *)notificaton {
    NSDictionary *dic = [notificaton userInfo];
    NSIndexPath *indexPath = [dic objectForKey:@"type"];
    
    switch (indexPath.row) {
        case 0:
            self.midType = CL_JSTL;
            break;
        case 1:
            self.midType = CL_XSDDWM;
            break;
        case 2:
            self.midType = CL_DGEDQZ;
            break;
        case 3:
            self.midType = CL_WX;
            break;
        case 4:
            self.midType = CL_YZWM;
            break;
        case 5:
            self.midType = CL_YZYM;
            break;
        case 6:
            self.midType = CL_YZWM_Z;
            break;
        case 7:
            self.midType = CL_YZYM_Z;
            break;
        case 8:
            self.midType = CL_OM;
            break;
        case 9:
            self.midType = CL_OM_Z;
            break;
        default:
            break;
    }

    // 主视图
    CGRect frame = self.middleViewCtrl.view.frame;
    [self.middleViewCtrl.view removeFromSuperview];
    self.middleViewCtrl = [self createMiddleCtrl];
    self.middleViewCtrl.view.frame = frame;
    [self.view insertSubview:self.middleViewCtrl.view aboveSubview:self.leftViewCtrl.view];
    [self moveToCenterWithLeftSelect];
}

// 左侧视图点击后，中间视图移动
- (void)moveToCenterWithLeftSelect {
    
    CGRect frame = self.middleViewCtrl.view.frame;
    [UIView animateWithDuration:kSelectMoveAnimationTimes
                     animations:^{
                         self.middleViewCtrl.view.frame = CGRectMake(frame.origin.x + 50,
                                                                     frame.origin.y,
                                                                     frame.size.width,
                                                                     frame.size.height);
                     }
                     completion:^(BOOL finished){
                         if (finished) {
                             [self moveToCenterWithLeftSelectOne];
                             // 移除视图
                             UIControl *overView = (UIControl *)[self.middleViewCtrl.view viewWithTag:10086];
                             [overView removeFromSuperview];
                         }
                     }];
}

- (void)moveToCenterWithLeftSelectOne {
    CGRect frame = self.middleViewCtrl.view.frame;
    [UIView animateWithDuration:kSelectMoveAnimationTimeOne
                     animations:^{
                         self.middleViewCtrl.view.frame = CGRectMake(0,
                                                                     frame.origin.y,
                                                                     frame.size.width,
                                                                     frame.size.height);
                     }
                     completion:^(BOOL finished){
                     }];
}


@end
